package com.example.administrator.easy.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.easy.R;
import com.example.administrator.easy.adapter.GuangFlAdapter;
import com.example.administrator.easy.config.NetConfig;
import com.example.administrator.easy.model.GuangFlResult;
import com.example.administrator.easy.model.GuangFlRoot;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/14.
 */

public class GuangFlFragment extends Fragment implements AbsListView.OnScrollListener{

    private SwipeRefreshLayout swipeRefreshLayout;
    private ListView lv;

    private ArrayList<GuangFlResult> list;
    private GuangFlAdapter adapter;
    private boolean isLoad;
    private String d = "00";
    private String jie;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_guang_fl,null);
        initView(view);
        initData();
        setData();
        setListener();
        return view;
    }

    private void setListener() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //下拉刷新
                d = "00";
                setData();
            }
        });
        lv.setOnScrollListener(this);//
    }

    private void setData() {
        Log.e("Mainurl",NetConfig.WELFARE+d);
        x.http().get(new RequestParams(NetConfig.WELFARE+d), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("Mainfl",result+"");
                GuangFlRoot gr = new Gson().fromJson(result,GuangFlRoot.class);
                Log.e("Mainflgr",gr+"");
                Log.e("Mainflsize",gr.getResult().size()+"");
                for(int i=0;i<gr.getResult().size();i++){
                    list.add(gr.getResult().get(i));
                }
                adapter.notifyDataSetChanged();
                jie = list.get(gr.getResult().size()-1).getPublishDate();
                d= jie.substring(6,19);
                Log.e("Maind",d+"");
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("MainFl","onErrorfl");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initData() {
        list = new ArrayList();
        adapter = new GuangFlAdapter(getActivity(),list);
        lv.setAdapter(adapter);
    }

    private void initView(View view) {
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipp);
        lv = (ListView) view.findViewById(R.id.lvvv);
    }

    //上拉加载
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if(isLoad&&SCROLL_STATE_IDLE==scrollState){
            setData();
            Toast.makeText(getActivity(),"正在加载",Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if(firstVisibleItem+visibleItemCount==totalItemCount){
            isLoad = true;
        }
    }
}
